﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using HotelManager.Data_Transfer_Layer;

namespace HotelManager.Data_Access_Layer
{
    class chitiethoadonphongDAO
    {
        #region Variables
        private OleDbConnection conn;
        private string error;
        #endregion

        #region Properties
        public string Error
        {
            get { return error; }
            set { error = value; }
        }
        #endregion

        public chitiethoadonphongDAO()
        {
            error = "";
            try
            {
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\hotel.accdb";
                conn = new OleDbConnection(strConn);
                conn.Open();
            }
            catch (Exception ex)
            {
                Error = "Không kết nối được đến cơ sở dữ liêu";
            }
        }

        public chitiethoadonphongDTO[] get()
        {
            chitiethoadonphongDTO[] objs;
            objs = new chitiethoadonphongDTO[100];
            try
            {
                string sql = "SELECT * FROM chitiethoadonphong";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                int i = 0, n = dt.Rows.Count;
                objs = new chitiethoadonphongDTO[n];
                while (reader.Read())
                {
                    objs[i] = new chitiethoadonphongDTO();
                    objs[i].Hd_id = reader.GetInt32(0);
                    objs[i].P_id = reader.GetInt32(1);
                    objs[i].Cthdp_soluong = reader.GetInt32(2);
                    objs[i].Cthdp_gia = reader.GetInt32(3);
                    i++;
                }
            }
            catch (Exception ex)
            {

            }
            return objs;
        }

        public chitiethoadonphongDTO View_Detail(int hd_id)
        {
            chitiethoadonphongDTO obj = new chitiethoadonphongDTO();
            try
            {
                string sql = "SELECT * FROM chitiethoadonphong WHERE hd_id=" + hd_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    obj.Hd_id = reader.GetInt32(0);
                    obj.P_id = reader.GetInt32(1);
                    obj.Cthdp_soluong = reader.GetInt32(2);
                    obj.Cthdp_gia = reader.GetInt32(3);
                }
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
            return obj;
        }

        public void Delete(chitiethoadonphongDTO obj)
        {
            try
            {
                string sql = "DELETE FROM chitiethoadonphong WHERE hd_id=" + obj.Hd_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Insert(chitiethoadonphongDTO obj)
        {
            try
            {
                string sql = "INSERT INTO chitiethoadonphong VALUES(''," + obj.P_id + "," + obj.Cthdp_soluong + "," + obj.Cthdp_gia + ")";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Update(chitiethoadonphongDTO obj)
        {
            try
            {
                string sql1 = "UPDATE chitiethoadonphong SET p_id=" + obj.P_id + ",cthdp_soluong=" + obj.Cthdp_soluong + ",cthdp_gia=" + obj.Cthdp_gia + " WHERE hd_id=" + obj.Hd_id;
                OleDbCommand cmd = new OleDbCommand(sql1, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }
    }
}
